const shortid = require("shortid");
var express = require("express");
var router = express.Router();
const {
  findAccount,
  updateAccount,
  delAccount,
  findList,
} = require("../query/account");
const logger = require("../_unit/logs");

/* 查询页面列表. */
router.get("/account", async function (req, res) {
  // 查询数据
  let result = await findAccount();
  let accountData = result.data;
  res.render("accountList", { title: "Express", accountData });
});

router.get("/create", function (req, res) {
  res.render("create");
});

/* 提交表单数据 */
router.post("/account", async function (req, res) {
  req.body.id = shortid.generate();
  // 插入数据
  let result = await updateAccount(req.body);
  res.render("InfoPage", { msg: "创建成功!", url: "/account" });
});

router.get("/account/:id", async function (req, res) {
  // 查询数据
  let { id } = req.params;
  let result = await delAccount(id);
  logger.info(result, "result+");
  res.render("InfoPage", { msg: "删除成功!", url: "/account" });
});

router.get("/api/findList", async (req, res) => {
  let result = await findList();
  res.json(result);
});

module.exports = router;
